// pages/settings/index.js
Page({
  data: {
    settings: [
      {
        title: "账号与安全",
        items: [
          { name: "手机号", value: "135****6619", type: "arrow" },
          { name: "密码", value: "已设置", type: "arrow" },
          { name: "实名认证", value: "未认证", type: "arrow" }
        ]
      },
      {
        title: "通知设置",
        items: [
          { name: "消息通知", value: "开启", type: "switch", checked: true },
          { name: "活动提醒", value: "开启", type: "switch", checked: true },
          { name: "系统通知", value: "关闭", type: "switch", checked: false }
        ]
      },
      {
        title: "隐私设置",
        items: [
          { name: "个人资料", value: "公开", type: "arrow" },
          { name: "位置信息", value: "仅好友可见", type: "arrow" },
          { name: "动态可见", value: "所有人", type: "arrow" }
        ]
      },
      {
        title: "其他",
        items: [
          { name: "清除缓存", value: "128MB", type: "arrow" },
          { name: "关于我们", value: "v1.0.0", type: "arrow" },
          { name: "检查更新", value: "已是最新版本", type: "arrow" }
        ]
      }
    ]
  },

  onLoad: function () {
    wx.setNavigationBarTitle({
      title: '设置'
    })
  },

  onItemTap: function(e) {
    const item = e.currentTarget.dataset.item
    
    if (item.type === "switch") {
      return // 开关由组件自己处理
    }
    
    wx.showModal({
      title: item.name,
      content: `当前设置：${item.value}\n\n是否要修改此设置？`,
      showCancel: true,
      cancelText: '取消',
      confirmText: '修改',
      success: function(res) {
        if (res.confirm) {
          wx.showToast({
            title: '设置修改功能开发中',
            icon: 'none'
          })
        }
      }
    })
  },

  onSwitchChange: function(e) {
    const index = e.currentTarget.dataset.index
    const itemIndex = e.currentTarget.dataset.itemIndex
    const checked = e.detail.value
    
    const settings = this.data.settings
    settings[index].items[itemIndex].checked = checked
    settings[index].items[itemIndex].value = checked ? "开启" : "关闭"
    
    this.setData({ settings })
    
    wx.showToast({
      title: checked ? '已开启' : '已关闭',
      icon: 'success'
    })
  }
})
